/*
 * Created by JFormDesigner on Mon Jun 07 08:11:57 CST 2021
 */

package com.J1ay.carsys.gui.User.Dialog;

import java.awt.event.*;
import javax.swing.event.*;

import com.J1ay.carsys.gui.tishi.Success;
import com.J1ay.carsys.gui.tishi.Warning;
import com.J1ay.carsys.pojo.BowCar;
import com.J1ay.carsys.pojo.Bower;
import com.J1ay.carsys.pojo.Car;
import com.J1ay.carsys.pojo.Result;
import com.J1ay.carsys.service.Impl.UserServiceImpl;
import com.J1ay.carsys.service.UserService;
import com.J1ay.carsys.utils.IDutils;

import java.awt.*;
import java.util.Date;
import javax.swing.*;
import javax.swing.GroupLayout;

/**
 * @author Brainrain
 */
public class BowCarDialog extends JDialog {
    BowCar nowcar = new BowCar();
    UserService userService = new UserServiceImpl();
    String carPrice;
    float permNum;
    public BowCarDialog(Window owner, Car car, Bower bower) {
        super(owner);
        initComponents();
        nowcar.setCarId(car.getId());
        nowcar.setUId(bower.getUId());
        nowcar.setEmpId(bower.getEmpId());
        nowcar.setBowPrice(Float.parseFloat(car.getPrice()));
        if(bower.getBperm() == "vip") {
            permNum = (float) 0.9;
        }
        else if (bower.getBperm() == "svip") {
            permNum = (float) 0.85;
        }
        else{
            permNum = 1;
        }
        carPrice = car.getPrice();
        this.label1.setText("您目前选择的车是：车牌号为："+car.getNum());
        this.label3.setText("类型为："+car.getType() + "借车价格（天）为："+car.getPrice());
        this.label4.setText("折扣为："+permNum);
    }
    public void start() {
        this.setVisible(true);
    }

    private void dayStateChanged(ChangeEvent e) {
        // TODO add your code here
        float price;
        int days = Integer.parseInt(this.day.getValue().toString());
        if(Integer.parseInt(this.day.getValue().toString())>0){
            price = Float.parseFloat(carPrice) * permNum * days;
            this.total.setText("合计：" + price + "元");
        }
    }

    private void button1ActionPerformed(ActionEvent e) {
        // TODO add your code here
        if(Integer.parseInt(this.day.getValue().toString())>0){
            nowcar.setSn(IDutils.getId());
            nowcar.setBowDay(Integer.parseInt(this.day.getValue().toString()));
            nowcar.setBowCreate(new Date());
            nowcar.setBowEnd(new Date());
            Result result = userService.addRecord(nowcar);
            System.out.println(nowcar);
            if (200 == result.getStatus()){
                new Success(this,"借车成功!").start();
                this.dispose();
            }
            else{
                new Warning(this,result.getMsg()).start();
            }
        }
        else{
            new Warning(this,"请选择合理的借车天数！").start();
        }
    }
    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        label1 = new JLabel();
        label2 = new JLabel();
        day = new JSpinner();
        panel1 = new JPanel();
        total = new JLabel();
        button1 = new JButton();
        label4 = new JLabel();
        label3 = new JLabel();

        //======== this ========
        setTitle("\u5c0f\u6c7d\u8f66\u51fa\u79df\u7cfb\u7edf");
        setResizable(false);
        Container contentPane = getContentPane();

        //---- label1 ----
        label1.setText("\u60a8\u76ee\u524d\u9009\u62e9\u7684\u8f66\u4e3a\uff1a");

        //---- label2 ----
        label2.setText("\u8bf7\u9009\u62e9\u501f\u8f66\u5929\u6570");

        //---- day ----
        day.addChangeListener(new ChangeListener() {
            @Override
            public void stateChanged(ChangeEvent e) {
                dayStateChanged(e);
            }
        });

        //======== panel1 ========
        {
            panel1.setBackground(Color.lightGray);

            //---- total ----
            total.setText("\u5408\u8ba1:");
            total.setForeground(new Color(0, 51, 51));

            //---- button1 ----
            button1.setText("\u786e\u5b9a");
            button1.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    button1ActionPerformed(e);
                }
            });

            GroupLayout panel1Layout = new GroupLayout(panel1);
            panel1.setLayout(panel1Layout);
            panel1Layout.setHorizontalGroup(
                panel1Layout.createParallelGroup()
                    .addGroup(panel1Layout.createSequentialGroup()
                        .addGap(155, 155, 155)
                        .addComponent(button1)
                        .addContainerGap(173, Short.MAX_VALUE))
                    .addGroup(panel1Layout.createSequentialGroup()
                        .addGap(58, 58, 58)
                        .addComponent(total, GroupLayout.PREFERRED_SIZE, 90, GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 104, Short.MAX_VALUE)
                        .addComponent(label4, GroupLayout.PREFERRED_SIZE, 96, GroupLayout.PREFERRED_SIZE)
                        .addGap(50, 50, 50))
            );
            panel1Layout.setVerticalGroup(
                panel1Layout.createParallelGroup()
                    .addGroup(panel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(panel1Layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                            .addComponent(total, GroupLayout.PREFERRED_SIZE, 39, GroupLayout.PREFERRED_SIZE)
                            .addComponent(label4, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGap(28, 28, 28)
                        .addComponent(button1)
                        .addContainerGap(30, Short.MAX_VALUE))
            );
        }

        GroupLayout contentPaneLayout = new GroupLayout(contentPane);
        contentPane.setLayout(contentPaneLayout);
        contentPaneLayout.setHorizontalGroup(
            contentPaneLayout.createParallelGroup()
                .addGroup(contentPaneLayout.createSequentialGroup()
                    .addGap(53, 53, 53)
                    .addGroup(contentPaneLayout.createParallelGroup()
                        .addComponent(label1, GroupLayout.PREFERRED_SIZE, 293, GroupLayout.PREFERRED_SIZE)
                        .addGroup(contentPaneLayout.createSequentialGroup()
                            .addComponent(label2, GroupLayout.PREFERRED_SIZE, 107, GroupLayout.PREFERRED_SIZE)
                            .addGap(18, 18, 18)
                            .addComponent(day, GroupLayout.PREFERRED_SIZE, 133, GroupLayout.PREFERRED_SIZE))
                        .addComponent(label3, GroupLayout.PREFERRED_SIZE, 279, GroupLayout.PREFERRED_SIZE))
                    .addContainerGap(52, Short.MAX_VALUE))
                .addComponent(panel1, GroupLayout.Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        contentPaneLayout.setVerticalGroup(
            contentPaneLayout.createParallelGroup()
                .addGroup(contentPaneLayout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(label1, GroupLayout.PREFERRED_SIZE, 49, GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(label3, GroupLayout.PREFERRED_SIZE, 42, GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
                    .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                        .addComponent(label2, GroupLayout.PREFERRED_SIZE, 52, GroupLayout.PREFERRED_SIZE)
                        .addComponent(day, GroupLayout.PREFERRED_SIZE, 54, GroupLayout.PREFERRED_SIZE))
                    .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(panel1, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    private JLabel label1;
    private JLabel label2;
    private JSpinner day;
    private JPanel panel1;
    private JLabel total;
    private JButton button1;
    private JLabel label4;
    private JLabel label3;
    // JFormDesigner - End of variables declaration  //GEN-END:variables
}
